
package com.gopay.webtest.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans00700SignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.PayToBankApplyVO;
import com.gopay.webtest.vo.PayToBankQueryVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * 网关付款到银行Controller
 * 
 * @PayToBankController.java
 * @author fanghw
 * @2013-11-8 上午10:27:53 www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/paytobank")
@Controller
public class PayToBankController extends BaseController {

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "trans00700SignHandler")
    private Trans00700SignHandler trans00700SignHandler;

    /**
     * 跳转到申请页面
     * 
     * @return
     */
    @RequestMapping("/applyOp")
    public ModelAndView applyOp() {
    	ModelAndView mv = new ModelAndView();
    	
    	try {
    		Map<String, String> map = xmlDao.readData4Map("paytobank_applyOp", "apply");
    		
    		if (map.containsKey("gopayServerTime")) {
    			map.remove("gopayServerTime");
    			map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
    		}
    		if (map.containsKey("tranDateTime")) {
    			map.remove("tranDateTime");
    			map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
    		}
    		
    		mv = new ModelAndView("paytobank.applyOp");
    		mv.addObject("config", map);
    	} catch (Exception e) {
    		logger.error("跳转到申请页面", e);
    	}
    	
    	return mv;
    }
    /**
     * 跳转到申请页面
     * 
     * @return
     */
    @RequestMapping("/apply")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("paytobank_apply", "apply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("paytobank.apply");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到申请页面", e);
        }

        return mv;
    }

    /**
     * 执行申请
     * 
     * @return
     */
    @RequestMapping("/doApplyOp")
    public ModelAndView doApplyOp(@ModelAttribute PayToBankApplyVO applyVo) {
    	ModelAndView mv = new ModelAndView();
    	
    	try {
    		
    		// 签名
    		trans00700SignHandler.sign(applyVo.getTranCode()+"_"+applyVo.getVersion(), "signType", "signValue", applyVo, getRequest());
    		
    		mv.setViewName("paytobank.doApplyOp");
    		mv.addObject("applyVo", applyVo);
    	} catch (Exception e) {
    		logger.error("执行申请", e);
    	}
    	
    	return mv;
    }
    /**
     * 执行申请
     * 
     * @return
     */
    @RequestMapping("/doApply")
    public ModelAndView doApply(@ModelAttribute PayToBankApplyVO applyVo) {
        ModelAndView mv = new ModelAndView();

        try {

            // 签名
            trans00700SignHandler.sign(applyVo.getTranCode()+"_"+applyVo.getVersion(), "signType", "signValue", applyVo, getRequest());

            mv.setViewName("paytobank.doApply");
            mv.addObject("applyVo", applyVo);
        } catch (Exception e) {
            logger.error("执行申请", e);
        }

        return mv;
    }

    /**
     * 跳转到查询页面
     * 
     * @return
     */
    @RequestMapping("/query")
    public ModelAndView query() {
        ModelAndView mv = new ModelAndView();

        try {

            Map<String, String> map = xmlDao.readData4Map("paytobank_query", "query");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            
            
            // 提交时间开始
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            String tranBeginDate = DateUtils.format(calendar.getTime(), "yyyyMMddHHmmss");
            map.put("tranBeginDate", tranBeginDate);
            map.put("tranEndDate", DateUtils.format(new Date(), "yyyyMMddHHmmss"));

            mv = new ModelAndView("paytobank.query");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到查询页面", e);
        }

        return mv;
    }

    /**
     * 执行查询
     * 
     * @return
     */
    @RequestMapping("/doQuery")
    public ModelAndView doQuery(@ModelAttribute PayToBankQueryVO queryVo) {
        ModelAndView mv = new ModelAndView();

        try {
            // 签名
            trans00700SignHandler.sign(queryVo.getTranCode()+"_"+queryVo.getVersion(), "signType", "signValue", queryVo, getRequest());

            mv.setViewName("paytobank.doQuery");
            mv.addObject("queryVo", queryVo);
        } catch (Exception e) {
            logger.error("执行查询", e);
        }

        return mv;
    }
}
